package edu.auctionmart

import org.joda.time.*
import org.jadira.usertype.dateandtime.joda.*

class Bid implements Comparable {

	Auction auction
	User userAccount
	Float amount
	DateTime time

	static constraints = {
		auction(nullable: false)
		userAccount(nullable: false)
		amount(nullable: false, validator: {val, obj ->
			val >= obj.auction.startBid
		})
		time(nullable: false)
	}

	static belongsTo = [auction: Auction]

	String toString() {
		"${amount}"
	}

	@Override
	public int compareTo(Object o) {
		Bid otherBid = (Bid) o;
		if (this.amount < otherBid.amount) {
			return -1;
		} else if (this.amount > otherBid.amount) {
			return 1;
		} else if (this.time.isBefore(otherBid.time)) {
			return 1;
		} else if (this.time.isAfter(otherBid.time)) {
			return -1;
		}
		return 0;
	}
}
